#include "filelistcontextmenu.h"
#include <QDebug>

FileListContextMenu::FileListContextMenu(QObject* parent)
    : QObject(parent)
    , m_currentContextIndex(-1)
{
}

QMenu* FileListContextMenu::createUnifiedMenu(const QList<int>& selectedIndices, int contextIndex)
{
    m_currentSelectedIndices = selectedIndices;
    m_currentContextIndex = contextIndex;


    QMenu* menu = new QMenu();

    // === 一级菜单结构 ===
    addExportOperations(menu, selectedIndices);
    menu->addSeparator();

    addFileOperations(menu, selectedIndices);
    menu->addSeparator();

    addArrangeOperations(menu, selectedIndices);
    menu->addSeparator();

    addSortOperations(menu, selectedIndices);
    menu->addSeparator();

    addSelectionOperations(menu);

    return menu;
}

void FileListContextMenu::addExportOperations(QMenu* menu, const QList<int>& selectedIndices)
{
    // 暂时去掉PDF导出功能
    Q_UNUSED(selectedIndices)
    Q_UNUSED(menu)

    // 注释：暂时禁用文件列表的PDF导出功能
    // 用户可以通过主界面的导出功能进行PDF导出
}

void FileListContextMenu::addFileOperations(QMenu* menu, const QList<int>& selectedIndices)
{
    int count = selectedIndices.size();

    if (count == SINGLE_SELECTION) {
        QAction* removeAction = menu->addAction("移除文件");
        removeAction->setIcon(QIcon(":/icons/remove.png"));
        connect(removeAction, &QAction::triggered, this, &FileListContextMenu::handleRemoveFiles);
    } else if (count > 1) {
        QAction* removeAction = menu->addAction(QString("移除 %1 个文件").arg(count));
        removeAction->setIcon(QIcon(":/icons/remove.png"));
        connect(removeAction, &QAction::triggered, this, &FileListContextMenu::handleRemoveFiles);
    }
}

void FileListContextMenu::addArrangeOperations(QMenu* menu, const QList<int>& selectedIndices)
{
    if (selectedIndices.isEmpty()) return;

    QAction* moveTopAction = menu->addAction("移到顶部");
    moveTopAction->setIcon(QIcon(":/icons/move_top.png"));
    connect(moveTopAction, &QAction::triggered, this, &FileListContextMenu::handleMoveToTop);

    QAction* moveBottomAction = menu->addAction("移到底部");
    moveBottomAction->setIcon(QIcon(":/icons/move_bottom.png"));
    connect(moveBottomAction, &QAction::triggered, this, &FileListContextMenu::handleMoveToBottom);
}

void FileListContextMenu::addSortOperations(QMenu* menu, const QList<int>& selectedIndices)
{

    if (selectedIndices.size() < 1) {
        return; // 临时改为1个文件也能排序（用于测试）
    }

    int count = selectedIndices.size();

    QAction* naturalSortAction = menu->addAction(QString("智能排序 (%1个)").arg(count));
    naturalSortAction->setIcon(QIcon(":/icons/sort_natural.png"));
    naturalSortAction->setToolTip("按文件名自然排序（智能识别数字）");
    connect(naturalSortAction, &QAction::triggered, this, &FileListContextMenu::handleNaturalSort);

    QAction* reverseSortAction = menu->addAction(QString("反向排序 (%1个)").arg(count));
    reverseSortAction->setIcon(QIcon(":/icons/sort_reverse.png"));
    reverseSortAction->setToolTip("按当前顺序反向排列");
    connect(reverseSortAction, &QAction::triggered, this, &FileListContextMenu::handleReverseSort);
}

void FileListContextMenu::addSelectionOperations(QMenu* menu)
{
    QAction* selectAllAction = menu->addAction("全选");
    selectAllAction->setIcon(QIcon(":/icons/select_all.png"));
    connect(selectAllAction, &QAction::triggered, this, &FileListContextMenu::handleSelectAll);

    if (!m_currentSelectedIndices.isEmpty()) {
        QAction* clearSelectionAction = menu->addAction("取消选择");
        clearSelectionAction->setIcon(QIcon(":/icons/clear_selection.png"));
        connect(clearSelectionAction, &QAction::triggered, this, &FileListContextMenu::handleClearSelection);
    }
}

// === 槽函数实现 ===

void FileListContextMenu::handleRemoveFiles()
{
    emit removeFilesRequested(m_currentSelectedIndices);
}

void FileListContextMenu::handleExportSingle()
{
    emit exportSingleRequested(m_currentContextIndex);
}

void FileListContextMenu::handleExportMultiple()
{
    emit exportMultipleRequested(m_currentSelectedIndices);
}

void FileListContextMenu::handleExportMerged()
{
    emit exportMergedRequested(m_currentSelectedIndices);
}

void FileListContextMenu::handleMoveToTop()
{
    emit moveToTopRequested(m_currentSelectedIndices);
}

void FileListContextMenu::handleMoveToBottom()
{
    emit moveToBottomRequested(m_currentSelectedIndices);
}

void FileListContextMenu::handleSelectAll()
{
    emit selectAllRequested();
}

void FileListContextMenu::handleClearSelection()
{
    emit clearSelectionRequested();
}

void FileListContextMenu::handleNaturalSort()
{
    emit naturalSortRequested(m_currentSelectedIndices);
}

void FileListContextMenu::handleReverseSort()
{
    emit reverseSortRequested(m_currentSelectedIndices);
}